package echat.future.cn.echat.my.view;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.HashMap;
import java.util.Map;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityWebViewBinding;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

/**
 * WebView
 */
public class WxPayActivity extends BaseActivity<ActivityWebViewBinding>{


    boolean isFirst = true;

    @Override
    protected void onResume() {
        super.onResume();
        if(isFirst){
            isFirst = false;
        }else{
            //调用查询接口
            new HttpUtilsRequst<String>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).wxResult(getIntent().getStringExtra("orderNumber")))
                    .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<String>() {
                        @Override
                        public void onSuccss(String s) {
                            toast("支付成功");
                            finish();
                        }
                    }).setmOnRequstFaileListener(new HttpUtilsRequst.OnRequstFaileListener() {
                @Override
                public void onFaile(int code, String msg) {
                    toast(msg);
                    finish();
                }
            })
                    .start();
        }
    }

    @Override
    public int layoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    public IBaseNavagation navagation() {
        return new AdapterNavagation(this).setTitle("支付", 16, R.color.white).
                setBack(R.drawable.btn_back).setNavagationBackgroudColor(R.color.color_161823);
    }

    @Override
    public void initView() {
        String url = getIntent().getStringExtra("url");
        Map<String,String> extraHeaders = new HashMap<>();
        extraHeaders.put("Referer", "https://eliao-tv.com");//例如 http://www.baidu.com ))
        bind.webView.loadUrl(getIntent().getStringExtra("url"),extraHeaders);// 加载url，也可以执行js函数
        bind.webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try{
                    if(url.startsWith("weixin://")){
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }
                }catch (Exception e){
                    return false;
                }

                return super.shouldOverrideUrlLoading(view, url);
            }
        });
//        mWebView.setWebChromeClient(new SafeWebChromeClient());// 设置 WebChromeClient


        //声明WebSettings子类
        WebSettings webSettings =  bind.webView.getSettings();

        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);


        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
    }

    @Override
    protected void onDestroy() {
        bind.webView.destroy();
        super.onDestroy();
    }

    public  class SafeWebViewClient extends WebChromeClient {

    }
}
